package xyz.jfshare.netty.study.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xyz.jfshare.netty.study.server.dao.FriendMapper;
import xyz.jfshare.netty.study.server.dao.UserMapper;
import xyz.jfshare.netty.study.server.entity.Friend;
import xyz.jfshare.netty.study.server.entity.User;
import xyz.jfshare.netty.study.server.service.FriendService;

import java.util.Iterator;
import java.util.List;

@Service
public class FriendServiceImpl extends ServiceImpl<FriendMapper, Friend> implements FriendService {

    @Autowired
    private FriendMapper friendMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> getFriendByUserId(int id) {
        return friendMapper.getFriendByUserId(id);
    }

    @Override
    public List<User> getNotFriendByUserId(int id) {
        List<User> friends = getFriendByUserId(id);
        List<User> list = userMapper.selectList(
                new QueryWrapper<User>()
                        .eq("is_delete",0)
                        .ne("id",id)
        );
        Iterator<User> iterator = list.iterator();
        while (iterator.hasNext()){
            User user = iterator.next();
            boolean exists = false;
            for (User friend : friends) {
                if (user.getNickName().equals(friend.getNickName())){
                    exists = true;
                    break;
                }
            }
            if (exists){
                iterator.remove();
            }
        }
        return list;
    }

    @Override
    public int insert(Friend friend) {
        return friendMapper.insert(friend);
    }
}
